package com.chhuang.ewcrm.application;

import com.chhuang.ewcrm.api.AppRequest;
import com.chhuang.ewcrm.config.PropertiesUtil;
import com.chhuang.ewcrm.cons.Constants;
import com.chhuang.ewcrm.utils.MyPath;
import javafx.application.Application;
import javafx.event.Event;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.stage.Screen;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Optional;

/**
 * 定义主界面显示
 * @author chhuang
 * @create 2020-09-29-15:57
 */
public class RootApp extends Application {
    private static Logger logger = LoggerFactory.getLogger(RootApp.class);

    //创建 的一个对象
    private static RootApp instance = new RootApp();

    //让构造函数为 private，这样该类就不会被实例化
    private RootApp(){}

    //获取唯一可用的对象
    public static RootApp getInstance(){
        return instance;
    }

    @Override
    public void start(Stage stage) {
        try {
            Parent root = FXMLLoader.load(RootApp.class.getResource("/fxml/root.fxml"));
            stage.setTitle("称重过磅系统");//设置标题
            Scene scene = new Scene(root);
            scene.getStylesheets().add("org/kordamp/bootstrapfx/bootstrapfx.css");

            //ctrl+X
            scene.getAccelerators().put(new KeyCodeCombination(KeyCode.X, KeyCombination.SHORTCUT_DOWN), ()->{
//                logger.debug("RootStage:使用了快捷键ctrl+X");
                PropertiesUtil.setValue(MyPath.getConfigPath()+Constants.PROPERTIES_FILE_NAME, Constants.PS_BASE_URL_STR, "http://localhost:8080/");
                AppRequest.resetInstance();
            });

            //esc
            scene.getAccelerators().put(new KeyCodeCombination(KeyCode.ESCAPE), ()->
                close(null)
            );

            stage.getIcons().add(new Image("/img/guangfuu.png"));
            stage.setScene(scene);//设置场景
            stage.setResizable(true);//能改变窗口大小
            Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
            stage.setX(screenBounds.getMinX());
            stage.setY(screenBounds.getMinY());
            stage.setWidth(screenBounds.getWidth());
            stage.setHeight(screenBounds.getHeight());
            stage.setMaximized(true);

            stage.setOnCloseRequest(e->close(e));

            stage.show();

        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

    /**
     * 打开主页
     * @param oldStage
     */
    public static void open(Stage oldStage){
        instance.start(new Stage());
        oldStage.close();
    }

    /**
     * 重写关闭方法
     * @param event
     */
    public static void close(Event event){
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setTitle("确认关闭");
        alert.setHeaderText("您确定要关闭程序吗？");
        Optional<ButtonType> result = alert.showAndWait();
        if(result.get() == ButtonType.OK){
            System.exit(0);//退出时 关闭所有线程
        }else if(event!=null){
            event.consume();
        }
    }
}
